<?php
session_start();
/**
 * Created by PhpStorm.
 * User: Su
 * Date: 2014/10/11
 * Time: 11:50
 */
$m = I('m');
$a = I('a');
/**
 * 获取页面传递的变量
 * @param $name 变量名称
 * @return string 变量的值
 * @since 1.0 2014-10-11 by sutroon
 */
function I($name)
{
    if (isset($_GET[$name])) {
        return $_GET[$name];
    }
    if (isset($_POST[$name])) {
        return $_POST[$name];
    }
    return '';
}

/**
 * 提示并跳转
 * @param $msg
 * @param string $url
 * @since 1.0 2014-10-11 by sutroon
 */
function result($result, $msg, $url = '')
{
    if ($result === true) {
        die('<script>alert("'.$msg.'");location.href="'.$url.'";</script>');
    } else {
        die('<script>alert("'.$msg.'");history.back();</script>');
    }
}

/**
 * 弹出保存文件对话框
 * @param $default_file_name
 * @param $content
 * @since 1.0 2014-10-11 by sutroon
 */
function sofunc_save_file_dialog($default_file_name, $content)
{
    header("Content-Type: application/force-download");
    header('Content-Disposition: attachment; filename="' . $default_file_name . '"');
    echo $content;
    exit(0);
}

/**
 * 打开数据库连接
 * @param string $host
 * @param string $user
 * @param string $password
 * @param string $database
 * @return resource
 * @since 1.0 2014-9-9 by sutroon
 */
function open_conn($host = null, $user = null, $password = null, $database = null)
{
    if (!$host) {
        $host = 'localhost';
    }
    if (!$user) {
        $user = 'xcall';
    }
    if (!$password) {
        $password = 'root!@#';
    }
    if (!$database) {
        $database = 'cetdb';
    }
    if (!in_array('mysql', get_loaded_extensions())) {
        die('系统不支持mysql数据库扩展，请联系管理员开启!');
    }
    $db = mysql_connect($host, $user, $password) or die(mysql_error());
    mysql_select_db($database);
    mysql_query("set names 'utf8'");
    return $db;
}

/**
 * 检测登录
 * @since 1.0 2014-10-13 by sutroon
 */
function check_login()
{
    return isset($_SESSION['sqzs']);
}

/**
 * 处理登录
 * @param $u
 * @param $pwd
 * @since 1.0 2014-10-13 by sutroon
 */
function do_login($u, $pwd)
{
    if (!$u || !$pwd) {
        die('<script>alert("参数有误!");history.back();</script>');
    }
    if ($u !== "admin" || $pwd !== "admin123") {
        die('<script>alert("用户名或密码有误!");history.back();</script>');
    }
    $_SESSION['sqzs'] = $u;
}

/**
 * 处理注销登录
 * @since 1.0 2014-10-13 by sutroon
 */
function do_login_out()
{
    unset($_SESSION['sqzs']);
}

/**
 * @param $sql
 * @since 1.0 2014-10-13 by sutroon
 */
function show_table_text($sql)
{
    $result = mysql_query($sql) or die('query:' . mysql_error());
    if ($result === false) {
        die('result:' . mysql_error());
    }
    $str = '<table class="table-list" cellspacing="0">';
    $i = 1;
    while ($row = mysql_fetch_row($result, MYSQL_ASSOC)) {
        if ($i < 2) {
            $str .= '<tr>';
            foreach ($row as $k => $v) {
                $str .= "<th>$k</th>";
            }
            $str .= '</tr>';
        }
        $str .= '<tr>';
        foreach ($row as $k => $v) {
            $str .= "<td>$v</td>";
        }
        $str .= '</tr>';
        $i++;
    }
    mysql_free_result($result);
    $str .= '</table>';
    return $str;
}


/**
 * @since 1.0 2014-10-15 by sutroon
 */
function encrypt_rnd($data, $mode = 'ENCRYPT', $key = 'xcallEncrypt!@#')
{
    if ($mode == 'ENCRYPT') {
        srand((double)microtime() * 1000000);
        $rand = rand(1, 10000);
        $encrypt_key = md5($rand);
        $ctr = 0;
        $tmp = '';
        for ($i = 0; $i < strlen($data); $i++) {
            $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
            $tmp .= $encrypt_key[$ctr] . ($data[$i] ^ $encrypt_key[$ctr++]);
        }
        $tmp = encrypt_rnd_key($tmp, $key);
        return base64_encode($tmp);
    }
    if ($mode == 'DENCRYPT') {
        $txt = base64_decode($data);
        $txt = encrypt_rnd_key($txt, $key);
        $tmp = '';
        for ($i = 0; $i < strlen($txt); $i++) {
            $md5 = $txt[$i];
            $tmp .= $txt[++$i] ^ $md5;
        }
        return $tmp;
    }
    return $data;
}

/**
 * @since 1.0 2014-10-15 by sutroon
 */
function encrypt_rnd_key($data, $key)
{
    $ctr = 0;
    $tmp = '';
    for ($i = 0; $i < strlen($data); $i++) {
        $ctr = $ctr == strlen($key) ? 0 : $ctr;
        $tmp .= $data[$i] ^ $key[$ctr++];
    }
    return $tmp;
}